from selenium import webdriver
from pythonProject.common.conmon import SelectFunction
import urllib.request
from pytesseract import pytesseract
from PIL import ImageEnhance, Image
from pytesseract import image_to_string
import os
import pytesseract
from PIL import Image
from io import BytesIO
# from pytesseract import image_to_string
# im = Image.open(r"E:\Python\PycharmProjects\pythonProject\day_task\soures_img\20211107152436.png")
# # str_img =pytesseract.image_to_string(im,lang='chi_sim') #甚至成中文设置lang='chi_sim'
# str_img =pytesseract.image_to_string(im,lang='eng')
# print('识别为：%s' % str_img)
def binarizing(img, threshold):#将RGB彩图转为灰度图,二值化图片
  """传入image对象进行灰度、二值处理"""
  img = img.convert("L")  # 转灰度
  pixdata = img.load()
  w, h = img.size
  # 遍历所有像素，大于阈值的为黑色
  for y in range(h):
    for x in range(w):
      if pixdata[x, y] < threshold:
        pixdata[x, y] = 0
      else:
        pixdata[x, y] = 255
  img.save('result_1.jpg')
  return img









# im = Image.open(r"E:\Python\PycharmProjects\pythonProject\day_task\soures_img\20211107152551.png")
# binarizing(im,200)
im = Image.open(r"E:\Python\PycharmProjects\pythonProject\day_task\result_1.jpg")
# str_img =pytesseract.image_to_string(im,lang='chi_sim') #甚至成中文设置lang='chi_sim'
str_img =pytesseract.image_to_string(im,lang='eng')
print('识别为：%s' % str_img)
def depoint(img):
  """传入二值化后的图片进行降噪"""
  pixdata = img.load()
  w, h = img.size
  for y in range(1, h - 1):
    for x in range(1, w - 1):
      count = 0
      if pixdata[x, y - 1] > 245:  # 上
        count = count + 1
      if pixdata[x, y + 1] > 245:  # 下
        count = count + 1
      if pixdata[x - 1, y] > 245:  # 左
        count = count + 1
      if pixdata[x + 1, y] > 245:  # 右
        count = count + 1
      if pixdata[x - 1, y - 1] > 245:  # 左上
        count = count + 1
      if pixdata[x - 1, y + 1] > 245:  # 左下
        count = count + 1
      if pixdata[x + 1, y - 1] > 245:  # 右上
        count = count + 1
      if pixdata[x + 1, y + 1] > 245:  # 右下
        count = count + 1
      if count > 4:
        pixdata[x, y] = 255
  img.save('result.jpg')
  return img
# im = Image.open(r"E:\Python\PycharmProjects\pythonProject\day_task\result_1.jpg")
# depoint(im)
